<!DOCTYPE html>
<html>
<head>
    <meta content="text/html" charset="UTF-8">
    <title>jQuery EasyUI Demo</title>
    <link rel="stylesheet" type="text/css" href="css/themes/bootstrap/easyui.css">
    <link rel="stylesheet" href="css/themes/icon.css"/>
    <link rel="stylesheet" href="css/themes/color.css"/>
    <link rel="stylesheet" type="text/css" href="css/mycss.css"/>
    <script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
    <script type="text/javascript" src="js/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="js/jquery.datagrid.js"></script>
</head>
<body>
<script type="text/javascript">
try{
	destory($article_win);
	destory($article_type_add_win);
	destory($article_tree_menu);
	
	}catch(e){
		console.log(e)
	};
try{
	ue.destroy();
}catch(e){
	console.log(e);
}
</script>
<div style="display: none">
    <div id="article_win" style="padding: 10px">
		<form id="article_add_form" method="post">
		<input name="id" type="hidden" value="-1"/>
			<p>
				<label for="_weixin_article_title">标题:</label> <input
					class="easyui-textbox" id="_weixin_article_title" name="title"
					data-options="required:true,width:300" />
			</p>
			<p>
				<label for="type_combox_tree">分类:</label> <input
					id="type_combox_tree" name="typeid" />
			</p>
		</form>
		<div>
			<label for="_weixin_article_html">正文:</label>
			<script id="editor2" type="text/plain" style="width:950px;height:450px;"></script>
			<!-- <input class="easyui-textbox" id="html" type="text" name="html" data-options="required:true,multiline:true,width:400,height:200" /> -->
		</div>
    </div>
    <div id="article_tree_menu">
        <div data-options="name:'new'">新的分类</div>
        <div class="menu-sep"></div>
        <div data-options="name:'edit'">编辑分类</div>
        <div class="menu-sep"></div>
        <div data-options="name:'del'">删除分类</div>
        <div class="menu-sep"></div>
        <div>退出</div>
    </div>
    <div id="article_type_add_win">
    	<form id="article_type_add_form" action="" method="post">
    	<input name="id" type="hidden" value="-1"/>
    		<p>
				<label for="_article_type_title">分类名称:</label> 
			</p>
			<input class="easyui-textbox" id="_article_type_title" name="name"
					data-options="required:true,width:300" />
			<p>
				<label for="_type_combox_tree">父分类:</label>
			</p>
			<input
					id="_type_combox_tree" name="pid" />
			<p>
				<label for="_type_des">描述:</label> 
			</p>
			<input
					id="_type_des" class="easyui-textbox" name="des" data-options="required:true,multiline:true,width:300,height:100"/>
    	</form>
    </div>
</div>
<div class="easyui-layout" fit="true">
    <div data-options="region:'west',split:true,title:'文章分类'" style="width: 150px;padding: 5px">
        <ul id="article_tree_panel">
        </ul>
    </div>
    <div data-options="region:'center' ,border:false">
        <table id="articles"></table>
    </div>

</div>
<script type="text/javascript">
    var btn2 = $("<a href='javascript:btnss1()'>你好</a>").linkbutton({iconCls: 'icon-search', plain: true}).prop('outerHTML');
    //var $article_form_savebtn=$('#article_form_savebtn');
    //文章分类的树形下拉框
    var $tye_combox_tree=$('#type_combox_tree');

    var $article_tree_menu = $('#article_tree_menu');
    
    var $articles = $('#articles');
    var $article_win = $('#article_win');
    var $article_tree_panel = $('#article_tree_panel');
    var $article_add_form = $('#article_add_form');
    var $article_type_add_win=$('#article_type_add_win');
    var $article_type_add_form=$('#article_type_add_form');
    var $_type_combox_tree=$('#_type_combox_tree');
	var articleindex=-1;
	$article_type_add_win.dialog({
		width: 540,
        height: 350,
        minimizable:true,
        maximizable:true,
        resizable:true,
        collapsible:true,
        title:'添加分类',
        draggable: true,
        closed: true,
        iconCls : 'icon-add',
        buttons:[
            {
            	iconCls:'icon-save',
				text:'保存',
				handler:function(){
					if($article_type_add_form.form('validate')){
						var datas=$article_type_add_form.serializeArray();
	            		//datas.push({name:'html',value:ue.getContent()});
	            		console.log(datas);
	            		$.post('atype/api?method=add',datas,function(data){
	            			
	        				if(data.success){
	        					showMsg(0,'请求成功');
	        					setTimeout(function(){$article_tree_panel.tree('reload');console.log('roload书');}, 1500);
	        				}
	        			},'json');
					}
					
				}
            },
            {
            	iconCls:'icon-cancel',
				text:'取消',
				handler:function(){
					$article_type_add_win.dialog('close');
				}
            }
        ]
	});
    $article_tree_menu.menu({
        onClick: function (menu) {
        	var node=$article_tree_menu.menu('options').node;
        	console.log(node);
            switch (menu.name)
            {
                case 'new':
                	$article_type_add_win.dialog('setTitle','添加分类');
                	$article_type_add_form.form('clear');
                	$article_type_add_win.dialog('open');
                    break;
                case 'edit':
                	$article_type_add_win.dialog('setTitle','编辑分类');
                	$article_type_add_form.form('load','atype/api?method=byid&id='+node.id);
                	$article_type_add_win.dialog('open');
                	break;
                case 'del':
                	$.messager.confirm('确认','您确认想要删除吗，子类的分类文章都将会被删除？',function(r){
                		if(r){
                			$.get('atype/api?method=del&id='+node.id,undefined,function(data){
                				if(data.success){
                					setTimeout(function(){$article_tree_panel.tree('reload');}, 1500);		
                				}
                			},'json');
                		}
                	});
                	
                	break;
                default:
            }
        }
    });

    $tye_combox_tree.combotree({
        width:200,
        required: true
    });
    
    $_type_combox_tree.combotree({
    	width:200
    });
    
    $article_tree_panel.tree({
    	url:'atype/list.json',
        fit: true,
        border: false,
        onContextMenu: function (e, node) {
            e.preventDefault();
            console.log(node);
            // 显示快捷菜单
            $article_tree_menu.menu('show', {
                left: e.pageX,
                top: e.pageY
            });
            $article_tree_menu.menu('options').node=node;
        },
        onClick: function(node){
        	console.log(node);
        	$articles.datagrid('load', {
            	typeid: node.id
            });
        	/* if ($article_tree_panel.tree('isLeaf', node.target)) {
                
            } */
        },
        onAfterEdit:function(node){
        	console.log('结束编辑');
        	console.log(node);
        }
    });
	$.get('atype/list.json',undefined,function(d){
		//$article_tree_panel.tree('loadData',data);
		console.log(d);
		var data1=$.extend(true,[],d);
		var data2=$.extend(true,[],d);
		console.log(data1);
		$_type_combox_tree.combotree('loadData',data1);
		$tye_combox_tree.combotree('loadData',data2);
    },'json');
    $article_win.dialog({
        width: 1000,
        height: 600,
        minimizable:true,
        maximizable:true,
        resizable:true,
        collapsible:true,
        title:'添加文章',
        draggable: true,
        closed: true,
        iconCls : 'icon-add',
        //buttons : "#article_win_btn",
        onBeforeLoad:function(){
            console.log('jia zai nei rong ');
        },
        onOpen:function(){
        	
        },
        buttons:[
            {
            	iconCls:'icon-save',
            	text:'保存',
            	handler:function(){
            		if($article_add_form.form('validate')){
            			var datas=$article_add_form.serializeArray();
                		datas.push({name:'html',value:ue.getContent()});
                		console.log(datas);
                		$.post('article/api?method=add',datas,function(data){
                			
            				if(data.success){
            					showMsg(0,'请求成功');
            				}
            			},'json');
            		}
            		
            	}
			},
			{
				iconCls:'icon-cancel',
				text:'取消',
				handler:function(){
					$article_win.dialog('close');
				}
			}
        ]
    });

    $.parser.parse($article_add_form);
    $.parser.parse($article_type_add_form);
    $articles.datagrid({
        url: 'article/list.json',
        saveUrl: 'save_user.php',
        delUrl: 'article/api?method=del',
        columns: [
            [
             	{field:'ck',checkbox:true},
                {field: 'id', title: 'NewsId'},
                {field: 'title', title: '标题', editor: {type: 'textbox', options: {required: true}}},
                {field: 'username', title: '作者'},
                {field: 'ctime',title:'修改时间', width: 180, formatter:function(value){
                	return new Date(value).toLocaleString();
                }},
                {field:'show',title:'是否隐藏',formatter:function(value){
                	return value?'是':'否';
                }},
                {field: 'btn', title: '操作', formatter: function () {
                    return btn2;
                }}
            ]
        ],
        onDblClickRow:function(){
        	
        },
        toolbar:[
            {
            	iconCls:'icon-add',
            	text:'添加',
            	handler:function(){
            		$article_win.window('setTitle','添加文章');
            		$article_add_form.form('clear');
            		ue.setContent('');
            		$article_win.window('open');
            	}
            },
            {
            	iconCls:'icon-save',
            	text:'接受更改',
            	handler:function(){
            		
            	}
            },
            {
            	iconCls:'icon-save',
            	text:'保存',
            	handler:function(){
            		
            	}
            },
            {
            	iconCls:'icon-remove',
            	text:'删除选中',
            	handler:function(){
            		$articles.datagrid('DelRows');
            	}
            }
        ]
    });
    $article_add_form.form({
        //url: 'data/test.json',
        onLoadSuccess:function(data){
        	console.log(data);
        	ue.setContent(data.html);
        }
    });
    /* //删除文章
    function del_articles() {
        var rows = $articles.datagrid("getChecked");
        $articles.datagrid('destroyRow', rows);
    } */
    //显示预览窗口
    function btnss1(e) {
        var row = $articles.datagrid('getSelected');
        console.log(row);
        $article_win.window('setTitle','编辑文章');
        $article_add_form.form('load','article/api?method=byid&id='+row.id);
        $article_win.window('open');
    }
    //初始化编辑器
    
    var ue = UE.getEditor('editor2');
    
</script>
</body>
<script type="text/javascript">
    alert("init");
</script>
<script type="text/javascript" src="js/easyui-lang-zh_CN.js"></script>
</html>